package com.junzhiit.nengji.cms.web.util.cookie;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.junzhiit.nengji.cms.web.util.aliyun.SmsUtil;

@Component
public class CookieUtils {
	
	private static Log logger = LogFactory.getLog(SmsUtil.class);
	
	public static final int COOKIE_MAX_AGE = 7 * 24 * 3600;
    public static final int COOKIE_HALF_HOUR = 30 * 60;
    public static String COOKIE_DOMAIN;
    
    @Value("${sys.domain}")
	public void setCookieDomain(String CookieDomain) {
    	CookieUtils.COOKIE_DOMAIN= CookieDomain;
	}

	/**
     * 根据Cookie名称得到Cookie对象，不存在该对象则返回Null
     * 
     * @param request
     * @param name
     * @return
     */ 
    public static Cookie getCookie(HttpServletRequest request, String name) { 
        Cookie[] cookies = request.getCookies();
        if (cookies==null || cookies.length==0 ) { 
            return null; 
        } 
        Cookie cookie = null; 
        for (Cookie c : cookies) {
            if (name.equals(c.getName())) { 
                cookie = c;
                break; 
            } 
        } 
        
        
        return cookie; 
    }
     
    /**
     * 根据Cookie名称直接得到Cookie值
     * 
     * @param request
     * @param name
     * @return
     */ 
    public static String getCookieValue(HttpServletRequest request, String name) { 
        Cookie cookie = getCookie(request, name); 
        if(cookie != null){
            return cookie.getValue();
        }
        return null; 
    }
     
    /**
     * 移除cookie
     * @param request
     * @param response
     * @param name 这个是名称，不是值
     */
    public static void removeCookie(HttpServletRequest request, 
            HttpServletResponse response, String name) { 
        if (null == name) { 
            return; 
        } 
        Cookie cookie = getCookie(request, name); 
        if(null != cookie){
        	cookie.setDomain(COOKIE_DOMAIN);
            cookie.setPath("/");
            cookie.setValue(""); 
            cookie.setMaxAge(0); 
            response.addCookie(cookie);
        }
    }
    
    
    /**
     * 清楚所有 cookie
     * @param request
     * @param response
     */
    public static void removeCookie(HttpServletRequest request, 
            HttpServletResponse response) { 
    	
    	Cookie[] cookies = request.getCookies();
    	
    	if(null==cookies){
    		return;
    	}
    	
    	for(Cookie c :cookies ){
            c = new Cookie(c.getName(), null);
            c.setDomain(COOKIE_DOMAIN);
            c.setPath("/");
            c.setMaxAge(0);
            response.addCookie(c);
        }
    }
     
    /**
     * 添加一条新的Cookie，可以指定过期时间(单位：秒)
     * 
     * @param response
     * @param name
     * @param value
     * @param maxValue
     */ 
    public static void setCookie(HttpServletResponse response, String name, 
            String value, int maxValue) { 
    	logger.info("domain:---->"+COOKIE_DOMAIN+"-------->");
        if (StringUtils.isBlank(name)) {
            return; 
        }
        if (null == value) {
            value = ""; 
        } 
        Cookie cookie = new Cookie(name, value);
        cookie.setDomain(COOKIE_DOMAIN);
        cookie.setPath("/"); 
        if (maxValue != 0) {
            cookie.setMaxAge(maxValue); 
        } else { 
            cookie.setMaxAge(COOKIE_HALF_HOUR); 
        } 
        response.addCookie(cookie);
    }
     
    /**
     * 添加一条新的Cookie，默认7天内过期时间
     * 
     * @param response
     * @param name
     * @param value
     */ 
    public static void setCookie(HttpServletResponse response, String name, 
            String value) { 
        setCookie(response, name, value, COOKIE_MAX_AGE); 
    } 
  
    /**
     * 将cookie封装到Map里面
     * @param request
     * @return
     */
    public static Map<String,Cookie> getCookieMap(HttpServletRequest request){ 
        Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
        Cookie[] cookies = request.getCookies();
        if(!(cookies.length==0)){
            for(Cookie cookie : cookies){
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }
}
